<template>
  <!-- 放置一个图标 -->
  <div style="display:inline-block">
    <svg-icon :icon-class="isFull ? 'exit-fullscreen' : 'fullscreen'" class="fullscreen" @click="fullScreen" />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFull: false // 控制全屏按钮图标的切换(要提前准备两张图片)
    }
  },
  // 处理ESC按键
  created() {
    // 如果当前是全屏状态，按下ESC会恢复到普通状态，而此时组件中的数据项却没有变成false,所以导致图标没有及时修改过来。
    // ESC会改变当前的全屏状态
    // screenfull是插件，它自带事件监听 on
    // screenfull.isFullscreen ：插件自带的属性，表示当前是否处于全屏状态
    // 解决方案：使用screenfull的事件监听
    screenfull.on('change', () => {
      console.log('当前是否是全屏', screenfull.isFullscreen)
      this.isFull = screenfull.isFullscreen
    })
  },
  methods: {
    fullScreen() {
      screenfull.toggle()
      this.isFull = !this.isFull
    }
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
